package com.hsy.managemoney.modules.web;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.deparse.dpframe.ui.activity.DPBaseActivity;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.managemoney.R;
import com.hsy.managemoney.modules.utils.CommonUtils;

/**
 * Created by MartinKent on 2016/8/12.
 */
public class DPWebViewFragment extends Fragment {

    protected DPWebView webView;
    protected DPWebView.Client client;
    protected FragmentActivity activity;
    protected String url;

    private LinearLayout webViewContainer;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        webViewContainer = (LinearLayout) inflater.inflate(R.layout.dp_wenview_fragment, null);
        webView = (DPWebView) webViewContainer.findViewById(R.id.web_view);
        return webViewContainer;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.activity = getActivity();
        client = createWebViewClient(webViewContainer);
        webView.setClient(client);

        loadUrl(url);
    }

    protected DPWebView.Client createWebViewClient(ViewGroup webViewContainer) {
        return new WebViewClient(getActivity(), webViewContainer);
    }

    public void startActivity(String url, String type, String title, int backButtonVisibility) {
        if (null == activity) {
            return;
        }
        Intent intent = new Intent(activity, DPWebViewActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("type", type);
        intent.putExtra("title", title);
        intent.putExtra("backButtonVisibility", backButtonVisibility);
        super.startActivity(intent);
    }

    public void loadUrl(@NonNull String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (null != webView) {
            String u = url;
            if (u.contains("?")) {
                u += ("&token=" + CommonUtils.getToken(activity));
            } else {
                u += ("?token=" + CommonUtils.getToken(activity));
            }
            DPLogUtil.d("url=" + u);
            webView.loadUrl(u);
        }
        this.url = url;
    }

    public static class WebViewClient extends BaseWebViewHandler {
        private final ViewGroup webViewContainer;
        private View webViewFailurePage;
        private boolean isErrorPage = false;
        private Activity activity;

        public WebViewClient(@NonNull Activity activity, ViewGroup webViewContainer) {
            super(activity);
            this.activity = (DPBaseActivity) activity;
            this.webViewContainer = webViewContainer;
        }

        public void startActivity(String url, String type, String title, int backButtonVisibility) {
            Intent intent = new Intent(activity, DPWebViewActivity.class);
            intent.putExtra("url", url);
            intent.putExtra("type", type);
            intent.putExtra("title", title);
            intent.putExtra("backButtonVisibility", backButtonVisibility);
            activity.startActivity(intent);
        }

        @Override
        public void onPageFinished(android.webkit.WebViewClient client, WebView view, String url) {
            super.onPageFinished(client, view, url);
            if (!isErrorPage) {
                getWebView().setVisibility(View.VISIBLE);
            }
            isErrorPage = false;
        }

        @Override
        public void onReceivedError(android.webkit.WebViewClient client, final WebView view, int errorCode, String description, String failingUrl) {
            isErrorPage = true;
            getWebView().setVisibility(View.GONE);
            getWebView().loadUrl("javascript:document.body.innerHTML='';");
//            this.webViewFailurePage = CommonUtils.showNetworkFailure(getWebView().getContext(), webViewContainer, 0, new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (null != webViewFailurePage.getParent()) {
//                        webViewContainer.removeView(webViewFailurePage);
//                    }
            getWebView().reload();
//                }
//            });
        }

        @Override
        public boolean shouldOverrideUrlLoading(android.webkit.WebViewClient client, WebView view, String url) {
//            startActivity(url, "", "", View.VISIBLE);
            view.loadUrl(url);
            return true;
        }

        @Override
        protected void onOpenWindow(String u, String type, String title, int v) {
            startActivity(u, type, title, v);
        }

        @Override
        protected void onCloseWindow() {
            activity.finish();
        }

        @Override
        protected void onShowShare(String title, String content, String url, String imgUrl, String surl) {
        }
    }

}